package com.control.sqlitemanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteManager {
	Context mContext;
	DataHelper helper;
	SQLiteDatabase Mysqlite;
	public static String DataName = "MyData.db";
	public static String t_Name = "Induction_cooker";
	public static String Id = "_id";
	public static String v_Name = "Name";
	public static int DataVersion = 1;
	private   StringBuffer inductionCooker_table = new StringBuffer("CREATE TABLE "+t_Name);
	
	private String getTable(){
		inductionCooker_table.append("(id INTEGER PRIMARY KEY autoincrement,");
		inductionCooker_table.append("code varchar(50),");
		inductionCooker_table.append("name varchar(50),");
		inductionCooker_table.append("Img_name varchar(50),");
		inductionCooker_table.append("Img_url varchar(50),");
		inductionCooker_table.append("Overtime varchar(50),");
		inductionCooker_table.append("power varchar(50),");
		inductionCooker_table.append("status char(2),");
		inductionCooker_table.append("ip varchar(50),");
		return inductionCooker_table.toString();
	}
	
	private class DataHelper extends SQLiteOpenHelper{

		public DataHelper(Context context) {
			super(context, DataName, null,DataVersion);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(getTable());
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS "+t_Name);            
			onCreate(db);
			
		}
			
	 }
	public SQLiteManager(Context context){
		mContext = context;
	}
	public void open(){
		helper = new DataHelper(mContext);
		Mysqlite = helper.getWritableDatabase();
		
	}
	public void close(){
		helper.close();
	}
	
	public void Insert(String values){
		ContentValues cv = new ContentValues();
		cv.put(v_Name,values);
		Mysqlite.insert(t_Name, Id,cv);
	}

	public Cursor fetchAllData()
	{
		return Mysqlite.query(t_Name, new String[] { Id, v_Name}, null, null, null, null, null);
		
	}
	public Cursor fetchData(int RowId){
		return Mysqlite.query(true,t_Name, new String[] { Id, v_Name}, "_id="+RowId, null, null, null,null,null);
	}
}